<template>
  <ModalTemplate @close="$emit('close')">
    <div slot="header">
      <h2 class="inline">全部作业统计</h2>
      <Checkbox class="mar10l" v-model="isShowAllQuestions"
        >显示所有问题</Checkbox
      >
    </div>
    <div class="all-work-list">
      <dl v-for="(item, index) in allMembers" :key="'member_' + index">
        <dt>
          {{ item.nickname }} ({{ item.qq }}) -------- 总数：{{
            isShowAllQuestions ? item.qaNums : item.qaNums - item.len100
          }}
        </dt>
        <dd>
          <Badge2
            v-for="(qa, qIndex) in item.qas"
            :key="'qa_' + index + '_' + qIndex"
            :name="qa.name"
            :text="qa.length"
            :color="qa.length <= 100 ? 'red' : 'green'"
            v-if="isShowAllQuestions || qa.length <= 100"
          ></Badge2>
        </dd>
      </dl>
      <Spin fix v-if="isLoading"></Spin>
    </div>
    <div slot="footer">
      <Button @click="$emit('close')">返回</Button>
    </div>
  </ModalTemplate>
</template>

<script>
import { cloneObj, splitPerson, splitQuetions } from "js/utils.js";
export default {
  data() {
    return {
      isLoading: false,
      isShowAllQuestions: true,
      allMembers: [],
    };
  },
  computed: {
    members() {
      return this.$store.getters.getMembers();
    },
    qqObj() {
      return this.$store.getters.getQQObj();
    },
    nickNameObj() {
      return this.$store.getters.getNickNameObj();
    },
  },
  watch: {},
  methods: {
    async initData() {
      if (this.members.length === 0) {
        this.$global.log("请先导入成员");
      }
      this.isLoading = true;
      let text = await this.$global.getFileText("base");
      if (!text) {
        this.$global.log("请选择大群文件");
        this.isLoading = false;
        return;
      }
      let allList = splitPerson(text);
      allList.sort((a, b) => (a.time > b.time ? 1 : -1));
      let searchList = [];
      for (let i = 0, n = allList.length; i < n; i++) {
        let item = allList[i];
        let content = item.content;
        if (!content) {
          continue;
        }
        if (item.qq) {
          if (!this.qqObj[item.qq]) {
            continue;
          }
          if (item.nickname !== this.qqObj[item.qq].nickname) {
            item.nickname = this.qqObj[item.qq].nickname;
          }
        } else {
          if (!this.nickNameObj[item.nickname]) {
            continue;
          }
          item.qq = this.nickNameObj[item.nickname].qq;
        }
        // 过滤题目
        let qas = splitQuetions(content);
        if (qas.length === 0) {
          continue;
        }
        item.qas = qas;
        searchList.push(item);
      }
      let qasObj = {};
      let qaNames = [];
      let maxQas = 0;
      for (let i = 0, n = searchList.length; i < n; i++) {
        let item = searchList[i];
        let qas = item.qas;
        if (!qasObj[item.qq]) {
          qasObj[item.qq] = [];
        }
        qasObj[item.qq].push(...qas);
        qas.map((qa) => {
          if (!qaNames.includes(qa.name)) {
            qaNames.push(qa.name);
          }
        });
      }
      maxQas = qaNames.length;

      let allMembers = [];
      for (let i = 0, n = this.members.length; i < n; i++) {
        let member = cloneObj(this.members[i]);
        let qas = qasObj[member.qq] || [];
        member.qas = this.filterRepeat(qas);
        let allNum = 0;
        let len100 = 0;
        let qaObj = {};
        for (let j = 0, m = member.qas.length; j < m; j++) {
          let qa = member.qas[j];
          qaObj[qa.name] = qa;
          allNum += 1;
          if (qa.length > 100) {
            len100 += 1;
          }
        }
        member.qaNums = allNum;
        member.len100 = len100;
        let list = [];
        for (let i = 1; i <= maxQas; i++) {
          let name = "Q" + i;
          if (qaObj[name]) {
            list.push(qaObj[name]);
          } else {
            list.push({
              name: name,
              length: 0,
            });
          }
        }
        member.qas = list;

        allMembers.push(member);
      }
      this.allMembers = allMembers;
      this.isLoading = false;
    },
    filterRepeat(qas) {
      let obj = {};
      for (let i = 0, n = qas.length; i < n; i++) {
        let item = qas[i];
        if (!obj[item.name]) {
          obj[item.name] = [];
        }
        obj[item.name].push(item);
      }
      let list = [];
      for (let name in obj) {
        let list2 = obj[name];
        if (list2.length > 1) {
          list2.sort((a, b) => (a.time < b.time ? 1 : -1));
        }
        list.push(list2[0]);
      }
      // list.sort((a, b) => (a.name > b.name ? 1 : -1));
      return list;
    },
  },
  mounted() {
    this.initData();
  },
};
</script>

<style lang="less">
.all-work-list {
  padding: 20px;
  dl {
    padding-bottom: 20px;
    border-bottom: 1px dotted #ccc;
    dt {
      font-size: 14px;
      font-weight: 600;
    }
  }
}
</style>
